package com.wuze.cloud.handler;

import com.wuze.cloud.exception.SysGlobalException;
import com.wuze.cloud.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: wuze
 * @description:
 * @date: 2023/02/16
 */
@Slf4j
@SuppressWarnings("ALL")
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        log.error("未知异常{}", e);
        return Result.validfail("未知异常:"+e.getMessage());
    }

    @ExceptionHandler(SysGlobalException.class)
    public Result exceptionHandler(SysGlobalException e) {
        log.error("code:{},msg:{}",e.getCode(),e.getMessage());
        return Result.validfail(e.getCode(),e.getMessage());
    }

    // validation 报错捕捉
    @ExceptionHandler(BindException.class)
    public Result exceptionHandler(BindException e) {
        log.error("handler --- BindException --- {}", e.getBindingResult().getFieldError().getDefaultMessage());
        return Result.validfail(e.getBindingResult().getFieldError().getDefaultMessage());
    }

}
